package com.hhl.flower.controller;

import com.hhl.flower.common.result.Result;
import com.hhl.flower.context.BaseContext;
import com.hhl.flower.pojo.vo.ShoppingCartVO;
import com.hhl.flower.service.ShoppingCartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user/shoppingCart")
@Api(tags = "用户端购物车相关接口")
@Slf4j
public class ShoppingCartController {

	@Autowired
	private ShoppingCartService shoppingCartService;

	@PostMapping("/add")
	@ApiOperation("添加商品到购物车")
	public Result add(@RequestParam("fid") Integer fid, @RequestParam("number") Integer number, @RequestParam("amount") Double amount) {
		Integer userId = BaseContext.getCurrentId();

		shoppingCartService.addToCart(userId, fid, number, amount);

		return Result.success();
	}

	@GetMapping("/list")
	@ApiOperation("获取用户购物车信息")
	public Result<List<ShoppingCartVO>> listByUserId() {
		Integer userId = BaseContext.getCurrentId();

		List<ShoppingCartVO> cartByUserId = shoppingCartService.getCartByUserId(userId);

		return Result.success(cartByUserId);
	}

	@PostMapping("/delete")
	@ApiOperation("删除购物车商品")
	public Result delete(@RequestParam("fid") Integer fid) {


		log.info("删除购物车商品：{}", fid);
		shoppingCartService.deleteFlowerFromCart(fid);


		return Result.success();
	}

	@PostMapping("/update/number")
	@ApiOperation("更新购物车商品数量")
	public Result update(@RequestParam("fid") Integer fid, @RequestParam("number") Integer number, @RequestParam("amount") Double amount) {

		shoppingCartService.updateCartNumber(fid, number, amount);

		return Result.success();
	}
}
